﻿using BLL;
using DataAccess;
using DevExpress.XtraEditors;
using QuanLyKhachSan.Validation;
using System;
using System.Linq;


namespace QuanLyKhachSan.FormThemCapNhat
{
    public partial class FormTaiKhoanCapNhat : XtraForm
    {
        public TaiKhoan taiKhoan = new TaiKhoan();
        public bool IsInserted = false;
        private TaiKhoanManager tkManager = new TaiKhoanManager();

        public FormTaiKhoanCapNhat()
        {
            InitializeComponent();
        }

        public FormTaiKhoanCapNhat(TaiKhoan taikhoan)
        {
            InitializeComponent();
            KhoiTaoTaiKhoan(taikhoan);
        }

        private void KhoiTaoTaiKhoan(TaiKhoan taikhoan)
        {
            txtMaTaiKhoan.Text = taikhoan.MaTaiKhoan.ToString();
            lueditMaNV.EditValue = taikhoan.MaNV;
            txtUsername.Text = taikhoan.Username;
            txtPassword.Text = taikhoan.Password;
            txtEmail.Text = taikhoan.EmailDangKy;
            deditDangNhapCuoi.EditValue = taikhoan.LanDangNhapCuoi;
            cbbTrangThai.Text = taikhoan.TrangThai;
            lueditMaNhom.EditValue = taikhoan.MaNhom;
        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            taiKhoan.MaNV = Convert.ToInt32(lueditMaNV.EditValue);
            taiKhoan.Username = txtUsername.Text;
            taiKhoan.Password = txtPassword.Text;
            taiKhoan.EmailDangKy = txtEmail.Text;
            taiKhoan.LanDangNhapCuoi = Convert.ToDateTime(deditDangNhapCuoi.EditValue);
            taiKhoan.TrangThai = cbbTrangThai.Text;
            taiKhoan.MaNhom = lueditMaNhom.EditValue.ToString();
            ValidateResult vResult = tkManager.Validate(taiKhoan);
            if (!vResult.Message.Any())
            {
                ActionResult aResult;
                if (String.Compare(btnThaoTac.Text, "Thêm", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    aResult = tkManager.Them(taiKhoan);
                }
                else
                {
                    taiKhoan.MaTaiKhoan = int.Parse(txtMaTaiKhoan.Text);
                    aResult = tkManager.CapNhat(taiKhoan);
                }
                if (!aResult.Exceptions.Any())
                {
                    IsInserted = true;
                    Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    XtraMessageBox.Show(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                XtraMessageBox.Show(s);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormTaiKhoanCapNhat_Load(object sender, EventArgs e)
        {
            lueditMaNhom.Properties.DataSource = new NhomTaiKhoanManager().LayDanhSach().Result.ToList();
            lueditMaNV.Properties.DataSource = new NhanVienManager().LayDanhSach().Result.ToList();
            Loading();
        }

        private void Loading()
        {
            if (String.Compare(btnThaoTac.Text, "Thêm", StringComparison.OrdinalIgnoreCase) == 0)
            {
                txtMaTaiKhoan.Text = "";
                lueditMaNV.EditValue = "";
                txtUsername.Text = "";
                txtPassword.Text = "";
                txtEmail.Text = "";
                deditDangNhapCuoi.EditValue = DateTime.Now;
                cbbTrangThai.SelectedIndex = 0;
                lueditMaNhom.EditValue = "";
            }
        }

        private void btnThemNhom_Click(object sender, EventArgs e)
        {
            FormNhomTaiKhoanCapNhat frmNhomTaiKhoanCapNhat = new FormNhomTaiKhoanCapNhat();
            frmNhomTaiKhoanCapNhat.ShowDialog();
            if (frmNhomTaiKhoanCapNhat.IsInserted)
            {
                XtraMessageBox.Show("Thêm nhóm tài khoản thành công");
                lueditMaNhom.Properties.DataSource = new NhomTaiKhoanManager().LayDanhSach().Result.ToList();
            }
        }
    }
}
